package com.tpyule.tpclass.web.Controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tpyule.tpclass.entity.bean.FailResultBean;
import com.tpyule.tpclass.entity.bean.ResultBean;
import com.tpyule.tpclass.entity.bean.SuccessResultBean;
import com.tpyule.tpclass.entity.pojo.Web;
import com.tpyule.tpclass.service.WebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zzz
 * @version 1.0
 * @date 2020/4/21 13:24
 */
@RestController
@RequestMapping("/web")
public class WebController extends BaseController {
    @Autowired
    WebService webService;
    @RequestMapping("/get")
    @ResponseBody
    public void getWeb(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            int webId = Integer.parseInt(request.getParameter("webId"));
            Web web=webService.getWeb(webId);
            if (web == null) {
                resultBean = new FailResultBean("失败");
            }else{
                resultBean = new SuccessResultBean(web,"成功");
            }
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
    @RequestMapping("/update")
    @ResponseBody
    public void updateWeb(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        ResultBean resultBean = null;
        try {
            String webStr = request.getParameter("webData");
            Web web = objectMapper.readValue(webStr, Web.class);
            int result=webService.updateWeb(web);
            if (result<=0) {
                resultBean = new FailResultBean("失败");
            }else{
                resultBean = new SuccessResultBean(web,"成功");
            }
        } finally {
            this.setAjaxResponseResult(response,resultBean);
        }
    }
}
